using System;
using System.Drawing;
using System.Windows.Forms;

namespace ImageWorkOrder
{
    public partial class MainForm : Form
    {
        private PictureBox pictureBox;
        private TextBox txtWorkOrderNo;
        private TextBox txtDescription;
        private Button btnUpload;
        private Button btnSave;
        private string currentImagePath;

        public MainForm()
        {
            InitializeComponent();
            InitializeUI();
        }

        private void InitializeComponent()
        {
            this.Text = "图片工单系统";
            this.Size = new Size(800, 600);
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void InitializeUI()
        {
            // 创建工单号输入框
            Label lblWorkOrderNo = new Label
            {
                Text = "工单号：",
                Location = new Point(20, 20),
                Size = new Size(60, 20)
            };
            this.Controls.Add(lblWorkOrderNo);

            txtWorkOrderNo = new TextBox
            {
                Location = new Point(90, 20),
                Size = new Size(200, 20)
            };
            this.Controls.Add(txtWorkOrderNo);

            // 创建描述输入框
            Label lblDescription = new Label
            {
                Text = "描述：",
                Location = new Point(20, 50),
                Size = new Size(60, 20)
            };
            this.Controls.Add(lblDescription);

            txtDescription = new TextBox
            {
                Location = new Point(90, 50),
                Size = new Size(200, 60),
                Multiline = true
            };
            this.Controls.Add(txtDescription);

            // 创建图片预览框
            pictureBox = new PictureBox
            {
                Location = new Point(20, 120),
                Size = new Size(400, 400),
                SizeMode = PictureBoxSizeMode.Zoom,
                BorderStyle = BorderStyle.FixedSingle
            };
            this.Controls.Add(pictureBox);

            // 创建上传按钮
            btnUpload = new Button
            {
                Text = "上传图片",
                Location = new Point(440, 120),
                Size = new Size(100, 30)
            };
            btnUpload.Click += BtnUpload_Click;
            this.Controls.Add(btnUpload);

            // 创建保存按钮
            btnSave = new Button
            {
                Text = "保存工单",
                Location = new Point(440, 160),
                Size = new Size(100, 30)
            };
            btnSave.Click += BtnSave_Click;
            this.Controls.Add(btnSave);
        }

        private void BtnUpload_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp|所有文件|*.*";
                openFileDialog.Title = "选择图片";

                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        currentImagePath = openFileDialog.FileName;
                        pictureBox.Image = Image.FromFile(currentImagePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"加载图片时出错：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(txtWorkOrderNo.Text))
            {
                MessageBox.Show("请输入工单号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (pictureBox.Image == null)
            {
                MessageBox.Show("请先上传图片", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            try
            {
                // 创建保存目录
                string saveDirectory = Path.Combine(Application.StartupPath, "WorkOrders");
                Directory.CreateDirectory(saveDirectory);

                // 保存工单信息
                string workOrderDirectory = Path.Combine(saveDirectory, txtWorkOrderNo.Text);
                Directory.CreateDirectory(workOrderDirectory);

                // 保存图片
                string imageExtension = Path.GetExtension(currentImagePath);
                string newImagePath = Path.Combine(workOrderDirectory, $"image{imageExtension}");
                File.Copy(currentImagePath, newImagePath, true);

                // 保存工单信息
                string infoPath = Path.Combine(workOrderDirectory, "info.txt");
                File.WriteAllText(infoPath, $"工单号：{txtWorkOrderNo.Text}\r\n描述：{txtDescription.Text}\r\n创建时间：{DateTime.Now}");

                MessageBox.Show("工单保存成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                ClearForm();
            }
            catch (Exception ex)
            {
                MessageBox.Show($"保存工单时出错：{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ClearForm()
        {
            txtWorkOrderNo.Clear();
            txtDescription.Clear();
            pictureBox.Image = null;
            currentImagePath = null;
        }
    }
} 